// 72
package org.hwadee.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hwadee.dto.FlowloadUtils;
import org.hwadee.entity.*;
import org.hwadee.service.CommunicationService;
import org.hwadee.service.MessageService;
import org.hwadee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private CommunicationService communicationService;
    @Autowired
    private UserService userService;

    @RequestMapping("/message")
    public String messageContent(HttpSession session, Model model,
                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                 @RequestParam(value = "id", defaultValue = "0") int id) {
        User user = (User)session.getAttribute("user");
        if(id<1) {
            id = communicationService.listCommunicationByTime(user.getUserId()).get(0).getSender();
        }
        model.addAttribute("id",id);
        List<Message> messages = messageService.listmessagesByUserId(user.getUserId(),id);
        String json = "[";
        for(Message message : messages) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(message.getSendTime());
            String i = "float:left";
            if(message.getSender() == user.getUserId()) {
                i = "float:right";
            }
            json += "{'float':'"+i+"','content':'"+message.getMessageContent()+"','time':'"+time+"'},";
        }
        json = json.substring(0, json.length() -1);
        json += "]";
        model.addAttribute("messages", json);
        User senderUser = userService.getUserById(id);
        return "Admin/Interest/communication";
    }

    @RequestMapping("/message/{page}")
    @ResponseBody
    public Object pageHelper(HttpSession session,Model model,
                             @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                             @PathVariable(value="page") int page) {
        User user = (User)session.getAttribute("user");
        PageHelper.startPage(page,pageSize);
        List<Communication> communications = communicationService.listCommunicationByTime(user.getUserId());
        PageInfo<Communication> pageInfo = new PageInfo<Communication>(communications);
        return FlowloadUtils.buildResult(pageInfo.getPages(),communications);
    }

    @RequestMapping(value = "/message/sendMessage", method = RequestMethod.POST)
    public Result sendMessage(HttpSession session, Model model,
                              @RequestParam(value = "receiverId") int receiverId,
                              @RequestParam(value = "context") String context) {
        User user = (User)session.getAttribute("user");
        String replacedStr = context.replace("\r\n","<br>");
        messageService.sendMessage(user.getUserId(), receiverId, replacedStr);
        return new Result(200);
    }
}
